package org.iabako.client.ui.form.extranet.signup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.init.Iabako;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.field.InputTextEmail;
import org.iabako.client.ui.layout.field.InputTextPassword;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.shared.entity.enumeration.IabakoPackage;
import org.iabako.shared.entity.user.User;
import org.iabako.shared.iservice.security.AuthenticateServiceAsync;

/**
 * Created by jose on 10/12/14.
 */
public class SignUpDoneForm extends Composite {

    interface MyUiBinder extends UiBinder<HTMLPanel, SignUpDoneForm> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    private AuthenticateServiceAsync authenticateServiceAsync = AuthenticateServiceAsync.Util.getInstance();

    @UiField
    LabelText signUpSuccessTitle;
    @UiField
    LabelText activationSuccessText;
    @UiField
    Button goToIabako;
    @UiField
    InputTextEmail email;
    @UiField
    InputTextPassword password;
    @UiField
    FormPanel signInForm;

    SignUpForm signUpForm;


    public SignUpDoneForm() {
        initWidget(binderForm.createAndBindUi(this));

        goToIabako.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                email.setValue(signUpForm.getUser().getEmail());
                password.setValue(signUpForm.getPlainPassword());
                signInForm.submit();
            }
        });

        //Spring security
        email.getInputField().setName("j_username");
        password.getInputField().setName("j_password");
        //We don't want user be asked for password reminder
        password.getInputField().getElement().setAttribute("type", "password");

        signInForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
            public void onSubmitComplete(FormPanel.SubmitCompleteEvent event) {
                authenticateServiceAsync.getAuthenticatedUser(new IabakoAsyncCallback<User>() {
                    public void onSuccess(User user) {
                        if (user != null) {
                            //Sign in success
                            Iabako.showHomePage();
                        } else {
                            NavigationController.getInstance().show(new IabakoDialogBox("sign_up_manual_redirection_title", "sign_up_manual_redirection_text"));
                        }
                        super.onSuccess(user);
                    }
                });
            }
        });
    }

    @Override
    public void setVisible(boolean visible) {
        if (visible) {
            signUpSuccessTitle.setLabel(Messages.getLabel("sign_up_form_success_title", signUpForm.getUser().getFirstName()));
            activationSuccessText.setHtmlLabel(Messages.getLabel("sign_up_form_success_text", signUpForm.getEnterprise().getName(), IabakoPackage.freeOneMonth.getLabel()));
        }
        super.setVisible(visible);
    }

    public void setSignUpForm(SignUpForm signUpForm) {
        this.signUpForm = signUpForm;
    }
}
